package com.xunxi.mimi.ui.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.xunxi.mimi.app_base.R;
import com.xunxi.mimi.constants.EventBusCode;
import com.xunxi.mimi.ui.dialog.LoadingDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.ButterKnife;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @author liyu
 */
public abstract class BaseFragment extends Fragment {

    protected String TAG = this.getClass().getSimpleName();

    private LoadingDialog mLoadingDialog;

    private View mLoadingView;

    private CompositeDisposable mCompositeDisposable;//rx管理器，用来取消订阅

    private boolean mHandleKeyboardVisible;

    private boolean mOnCreate;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.common_base_content, container, false);
        mLoadingView = rootView.findViewById(R.id.loading_content_ll);
        FrameLayout contentFl = (FrameLayout) rootView.findViewById(R.id.content);
        View contentView = inflater.inflate(getFragmentLayout(), null);
        contentFl.addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        mOnCreate = true;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (mOnCreate) {
            mOnCreate = false;
            afterViews();
        }
    }

    private void afterViews() {
        this.initPresenter();
        this.initView();
        this.initValue();
    }

    /**
     * Every fragment has to inflate a layout in the onCreateView method. We have added this method to
     * avoid duplicate all the inflate code in every fragment.
     */
    protected abstract int getFragmentLayout();

    /**
     * 初始化presenter
     *
     * @return
     */
    protected abstract void initPresenter();

    /**
     * 初始化view
     *
     * @return
     */
    protected abstract void initView();

    /**
     * 初始化数据
     *
     * @return
     */
    protected abstract void initValue();

    protected final View findViewById(int id) {
        return getView().findViewById(id);
    }

    /**
     * 显示进度条
     */
    public void dismissLoadingDialog() {
        if (isAdded() && mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    /**
     * 隐藏进度条
     */
    public void showLoadingDialog() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(getActivity());
        }
        mLoadingDialog.show();
    }

    /**
     * 显示进度条
     */
    public void dismissLoadingContentView() {
        if (isAdded() && mLoadingView != null) {
            mLoadingView.setVisibility(View.GONE);
        }
    }

    /**
     * 显示加载界面
     */
    public void showLoadingContentView() {
        if (mLoadingView != null) {
            mLoadingView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 启动页面跳转
     *
     * @param cls
     */
    public void startIntent(Class<?> cls) {
        this.startIntent(getActivity(), cls);
    }

    /**
     * 启动页面跳转
     *
     * @param cls
     */
    public void startIntent(Class<?> cls, Bundle bundle) {
        this.startIntent(getActivity(), cls, bundle);
    }

    /**
     * 启动页面跳转
     *
     * @param context
     * @param cls
     */
    public void startIntent(Context context, Class<?> cls) {
        this.startIntent(context, cls, null);
    }

    /**
     * 启动页面跳转
     *
     * @param context
     * @param cls
     * @param bundle
     */
    public void startIntent(Context context, Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(context, cls);

        if (bundle != null) {
            intent.putExtras(bundle);
        }

        context.startActivity(intent);
    }

    /**
     * 启动页面跳转 附带返回
     *
     * @param cls
     * @param requestCode
     */
    public void startActivityForResult(Class<?> cls, int requestCode) {
        Intent intent = new Intent(getActivity(), cls);
        startActivityForResult(intent, requestCode);
    }

    /**
     * 启动页面跳转 附带返回
     *
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public void startActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent(getActivity(), cls);
        intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
    }

    /**
     * eventBus可以重写，用来处理接收消息，
     *
     * @param event
     */
    @Subscribe
    public void onEventMainThread(Object event) {
        if (event instanceof Bundle) {
            int eventCode = ((Bundle) event).getInt(EventBusCode.EVENT_BUS_CODE);
            onEventMainThread(eventCode, (Bundle) event);
        }
    }

    /**
     * eventBus可以重写，用来处理接收消息，
     *
     * @param eventCode
     */
    public void onEventMainThread(int eventCode, Bundle bundle) {
    }

    protected void unSubscribe() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear();
        }
    }

    protected void addSubscribe(Disposable subscription) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(subscription);
    }

    /**
     * 启用软键盘状态监听
     */
    protected void enableKeyboardVisibilityHandle() {
        mHandleKeyboardVisible = true;
    }

    public boolean isHandleKeyboardVisible() {
        return mHandleKeyboardVisible;
    }

    /**
     * 处理软键盘的打开/关闭
     *
     * @param visibility
     */
    protected void onKeyboardVisibilityChanged(boolean visibility) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unSubscribe();
        EventBus.getDefault().unregister(this);
    }
}
